<?php
class Prof_RepController extends Zend_Controller_Action{
	private $_auth;
	public function init(){
		$this->_auth = Zend_Auth::getInstance();
	}
	public function addAction(){
		$request = $this->getRequest();
		$id_qcm = (int) $this->_getParam('qcm', null);
		$num_quest = (int) $this->_getParam('quest', null);
		$dbRep = new Application_Model_DbTable_Reponse();
		$form = new Prof_Form_Rep();
		
		if($this->getRequest()->isPost()){
			if($form->isValid($request->getPost()) && $id_qcm>0 && $num_quest>0){
				$values = $form->getValues();
				
				$values['id_qcm'] = $id_qcm;
				$values['num_quest'] = $num_quest;
				$values['num_rep'] = $dbRep->nextID($id_qcm, $num_quest);
				
				$dbRep->insert($values);
				$this->_helper->redirector('view','quest','prof',array('qcm'=>$id_qcm,'quest'=>$num_quest));
			}else{
				$this->view->form = $form;
				$errors = $form->getMessages();
				$messages = array();
				foreach ($errors as $err) {
					foreach($err as $mess){
						$messages[]=$mess;
					}
				}
				if($id_qcm<1 || $num_quest<1) $message[] = 'Invalid parameters';
				$this->view->errors = $messages;
			}
		}else{
			$this->view->form = $form;
		}
	}
	public function editAction(){
		$request = $this->getRequest();
		$id_qcm = (int) $this->_getParam('qcm', null);
		$num_quest = (int) $this->_getParam('quest', null);
		$num_rep = (int) $this->_getParam('rep', null);
		$db = new Application_Model_DbTable_Reponse();
		$form = new Prof_Form_Rep();
		
		if($this->getRequest()->isPost()){
			if($form->isValid($request->getPost())){
				$values = $form->getValues();

				$db->update($values, array('id_qcm = ?'=>$values['id_qcm'],'num_quest = ?'=>$values['num_quest'],'num_rep = ?'=>$values['num_rep']));
				$this->_helper->redirector('view','quest','prof',array('qcm'=>$id_qcm,'quest'=>$num_quest));
			}else{
				$this->view->form = $form;
				$errors = $form->getMessages();
				$messages = array();
				foreach ($errors as $err) {
					foreach($err as $mess){
						$messages[]=$mess;
					}
				}
				if($id_qcm<1 || $num_quest<1) $message[] = 'Invalid parameters';
				$this->view->errors = $messages;
			}
		}else{
		if($id_qcm>0 && $num_quest>0 && $num_rep>0){
				$form->populate(get_object_vars($db->find($id_qcm, $num_quest,$num_rep)));
				$this->view->form = $form;
			}else{
				$this->_helper->redirector('deny','error');
			}
		}
	}
	public function deleteAction(){
		$id_qcm = (int) $this->_getParam('qcm', null);
		$num_quest = (int) $this->_getParam('quest', null);
		$num_rep = (int) $this->_getParam('rep', null);
		
		if($id_qcm>0 && $num_quest>0 && $num_rep>0){
			$db = new Application_Model_DbTable_Reponse();
			$db->delete(array('id_qcm = ?'=>$id_qcm,'num_quest = ?'=>$num_quest,'num_rep = ?'=>$num_rep));
			$this->_helper->redirector('view','quest','prof',array('qcm'=>$id_qcm,'quest'=>$num_quest));
		}else{
			$this->_helper->redirector('deny','error');
		}
	}
}